package com.project.controller;

import com.project.dto.userToMerchant.OrderDto;
import com.project.dto.userToMerchant.OrderInfoDto;
import com.project.entity.*;
import com.project.service.ISysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/10/9 9:30
 */
@RestController
@RequestMapping("/sys-whatweneedcontroller")
public class SysWhatWeNeedController {

    @Autowired
    ISysOrderService orderService;

    //监听用户订单mq信道
    //@RabbitListener(queues = "UserQueue")
    public void updateNumber(OrderDto orderDto) {
        List<SysOrderDetail> orderDetails = new LinkedList<>();

        SysOrder order = new SysOrder();
        //订单编号
        order.setONumber(orderDto.getOrderCode());
        //商铺编号
        order.setStore(new SysStore().setStoreCode(orderDto.getShopCode()));
        //用户编号
        order.setUser(new SysUser().setUserCode(orderDto.getUserCode()));
        //订单送货地址
        order.setOAddress(orderDto.getReceiverAddress());
        //订单状态(商铺未接单)
        order.setOState(0);
        //下单时间
        order.setOTime(orderDto.getOrderTime());
        //订单总金额
        order.setOMoney(Integer.valueOf(orderDto.getOrderMoney()));
        //收货人电话
        order.setOReceiverPhone(orderDto.getReceiverPhone());
        //订单备注
        order.setODescription(orderDto.getDescription());
        //订单明细集合
        List<OrderInfoDto> list = orderDto.getInfoList();
        for (OrderInfoDto orderInfoDto : list) {
            SysOrderDetail orderDetail = new SysOrderDetail();
            orderDetail.setOrder(new SysOrder().setONumber(String.valueOf(orderInfoDto.getOrderCode())));

            SysProduct product = new SysProduct();
            product.setPkProductid(orderInfoDto.getProductId());
            product.setPName(orderInfoDto.getProductName());
            product.setPPrice(Integer.valueOf(orderInfoDto.getProductPrice()));
            orderDetail.setProduct(product);

            orderDetail.setNumber(orderInfoDto.getBuyNum());

            orderDetails.add(orderDetail);
        }

        order.setOrderDetails(orderDetails);


        orderService.addOrder(order);
        //todo  lyl处理订单业务

    }
}
